import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/login.vue'
import layout from '@/views/layout/layout.vue'
import bussiness from '@/views/layout/bussiness/bussiness'
import chart from '@/views/layout/chart/chart'
import question from '@/views/layout/question/question'
import subject from '@/views/layout/subject/subject'
import user from '@/views/layout/user/user'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store/index.js'
import { Message } from 'element-ui'
import { removeLocal } from '@/utils/local.js'


Vue.use(VueRouter)

const router = new VueRouter({
    routes: [{
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: login,
            meta:{
                title:'登录',
                rules: ["超级管理员","管理员", "老师", "学生"]
            }
        },
        {
            path: '/layout',
            component: layout,
            redirect: '/layout/subject',
            children: [
                {
                    path: '/layout/chart',
                    component: chart,
                    meta:{
                        title:'数据概览',
                        icon:'el-icon-user',
                        rules: ["超级管理员","管理员", "老师"]
                    }
                },
                {
                    path: '/layout/user',
                    component: user,
                    meta:{
                        title:'用户列表',
                        icon:'el-icon-notebook-2',
                        rules: ["超级管理员","管理员"]
                    }
                },
                {
                    path: '/layout/bussiness',
                    component: bussiness,
                    meta:{
                        title:'企业列表',
                        icon:'el-icon-pie-chart',
                        rules: ["超级管理员","管理员", "老师"]
                    }
                },
                {
                    path: '/layout/question',
                    component: question,
                    meta:{
                        title:'题库列表',
                        icon:'el-icon-edit-outline',
                        rules: ["超级管理员","管理员", "老师"]
                    }
                },
                {
                    path: '/layout/subject',
                    component: subject,
                    meta:{
                        title:'学科列表',
                        icon:'el-icon-office-building',
                        rules: ["超级管理员","管理员", "老师", "学生"]
                    }
                },
                
            ]
        }
    ]
})

router.beforeEach((to,from,next)=>{
    NProgress.start()
    if(!to.meta.rules.includes(store.state.role)){
        Message.erroe('非法访问，您已被退出登录')
        removeLocal('token')
        next('/login')
    }else{
        next()
    }
    from;
})

router.afterEach((to,from)=>{
    to;
    from;
    document.title=to.meta.title
    setTimeout(()=>{
        NProgress.done()
    },1000)
})


// 解决同一路由跳转时的报错
const originalPush=VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router